package com.kakaxi.mybilibili.module.home.drama.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.slider.library.SliderLayout;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.recyclerview.LuRecyclerView;
import com.github.jdsjlzx.recyclerview.LuRecyclerViewAdapter;
import com.github.jdsjlzx.util.LuRecyclerViewStateUtils;
import com.github.jdsjlzx.view.LoadingFooter;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.DramaHomeRecyAdapter;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.DramaRecommendItemBean;
import com.kakaxi.mybilibili.bean.DramaTotalItemBean;
import com.kakaxi.mybilibili.module.all.WebViewActivity;
import com.kakaxi.mybilibili.module.home.drama.presenter.DramaPresenter;
import com.kakaxi.mybilibili.module.home.drama.presenter.IDramaPresenter;

import java.util.List;

/**
 * 番剧主页面碎片
 * Created by kakaxi on 16/10/11.
 */

public class DramaHomeFragment extends BaseFragment implements IDramaView, DramaHomeRecyAdapter.OnItemClickListener {

    /** 番剧循环视图 **/
    private LuRecyclerView mDramaLRecycView;
    /** 番剧左循环列表视图适配器 **/
    private LuRecyclerViewAdapter mDramaViewAdapter;
    /** 番剧适配器 **/
    private DramaHomeRecyAdapter mDramaAdapter;
    /** 刷新控件 **/
    private SwipeRefreshLayout mSwipeRefreshLayout;

    /** 番剧控制器 **/
    private IDramaPresenter mDramaPresenter;

    /**  **/
    private static final int PAGE_SIZE = 10;

    public static DramaHomeFragment newInstance() {
        return new DramaHomeFragment();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler_new;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        // 初始化循环视图
        mDramaLRecycView = (LuRecyclerView) viewGroup.findViewById(R.id.lrecv_common_fragment);
        mDramaLRecycView.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mDramaAdapter = new DramaHomeRecyAdapter(getContext(), null);
        mDramaAdapter.setOnItemClickListener(this);
        mDramaViewAdapter = new LuRecyclerViewAdapter(mDramaAdapter);
        mDramaLRecycView.setAdapter(mDramaViewAdapter);
        mDramaLRecycView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                // 获取当前加载更多的状态
                LoadingFooter.State state = LuRecyclerViewStateUtils.getFooterViewState
                        (mDramaLRecycView);
                // 正在加载中不做处理
                if (state == LoadingFooter.State.Loading) {
                    return;
                }

                // 设置成正在加载状态, 并调用读取更多推荐数据
                LuRecyclerViewStateUtils.setFooterViewState(getActivity(), mDramaLRecycView,
                        PAGE_SIZE, LoadingFooter.State.Loading, null);
                mDramaPresenter.loadMoreDramaRecommendData(0);

            }
        });
        // 初始化刷新控件
        mSwipeRefreshLayout = (SwipeRefreshLayout) viewGroup.findViewById(R.id.srl_common_fragment);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mDramaPresenter.refreshDramaSerializaingData();
            }
        });
    }

    @Override
    protected void initData() {
        mDramaPresenter = new DramaPresenter(this);
        mDramaPresenter.initDramaSerializaingData();
    }

    @Override
    public void showDramaTotalData(DramaTotalItemBean dramaTotalItemBean) {
        // 把总数据设置到适配器中
        mDramaAdapter.setDramaTotalItemBean(dramaTotalItemBean);
    }

    @Override
    public void addDramaRecommendData(List<DramaRecommendItemBean> dramaRecommendItemBeanList) {
        mDramaAdapter.addDramaRecommendList(dramaRecommendItemBeanList);
        // 设置加载结束状态
        LuRecyclerViewStateUtils.setFooterViewState(getActivity(), mDramaLRecycView,
                PAGE_SIZE, LoadingFooter.State.TheEnd, null);
    }


    @Override
    public void showRefresh() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
    }


    @Override
    public void onBannerClick(SliderLayout sliderLayout, List<BannerItemBean> bannerItemBeanList) {
        BannerItemBean itemBean = bannerItemBeanList.get(sliderLayout.getCurrentPosition());
        Bundle bundle = new Bundle();
        bundle.putString(WebViewActivity.EXTRA_URL, itemBean.getLink());
        goActivity(WebViewActivity.class, bundle);
    }

    @Override
    public void onThreeBtnClick(View view, int what) {
        switch (what) {
            case 0:
                toastHandler("开发中。。。");
                break;
            case 1:
                break;
            case 2:
                break;
        }
    }

    @Override
    public void onTitleClick(View view, int what) {

    }

    @Override
    public void onDramaClick(View view, int pos) {

    }

    @Override
    public void onRecommendClick(View view, int pos) {

    }
}
